package com.example.win7.app_speak;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

@RequiresApi(api = Build.VERSION_CODES.M)
public class MainActivity extends AppCompatActivity {

    private MediaRecorder mRecoder;
    private MediaPlayer mMediaPlayer;
    private String fileName = null;
    private String filePath = null;
    private Button mButtonPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //重新设置界面大小
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 23) {
            int REQUEST_CODE_CONTACT = 101;
            String[] permissions = {Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
            //验证是否许可权限
            for (String str : permissions) {
                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    //申请权限
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                    init();
                }
            }
        }

        //设置sdcard的路径
        fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        filePath = fileName + "/audiorecordtest.3gp";

        init();
    }

    private void init() {
        //new出录音对象
        mRecoder = new MediaRecorder();

        //设置Audio的音频源为麦克风
        mRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置Audio录音的音频模式(输出格式)
        //  mRecoder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //   mRecoder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecoder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        //设置Audio录制音频的编码为AMR
        mRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        //设置录制的音频的存储路径

        mRecoder.setOutputFile(filePath);


        mMediaPlayer = new MediaPlayer();

        mButtonPlayer = findViewById(R.id.palyer);
        mButtonPlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
                mMediaPlayer.reset();
                try {
                    mMediaPlayer.setDataSource(filePath);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public void btnClick(View view) {
        switch (view.getId()) {
            case R.id.start:
                if (mRecoder == null) {
                    mRecoder = new MediaRecorder();
                    mRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecoder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mRecoder.setOutputFile(filePath);
                    try {
                        mRecoder.prepare();
                        mRecoder.start();
                    } catch (IllegalStateException e) {
                        System.out.print(e.getMessage());
                    } catch (IOException e) {
                        System.out.print(e.getMessage());
                    }

                }
                break;
            case R.id.stop:
                if (mRecoder != null) {
                    try {
                        mRecoder.stop();
                    } catch (IllegalStateException e) {
                        mRecoder = null;
                        mRecoder = new MediaRecorder();
                    }
                    mRecoder.release();
                    mRecoder = null;
                }
                break;
        }



    }


}


